import { defineStore } from 'pinia'

/* 购物车 */
export default defineStore('stobStore', {
	state() {
		return {
			view:{
				no: '',  // 订单号
				direct: false,
				items: [], // 商品
				coupon: {},  // 优惠券
				promote: {}, // 促销
				address: {
					consignee: '',
					mobile: '',
					dist_id: '',
					location: ''
				},
				invoice: {
					mobile: '',
					email: '',
					title: 0,
					content: 0
				},
				expr_type: 0,
				expr_fee: 0,
				item_fee: 0,
				derate_fee: 0,
				pay_fee: 0,
				total_fee: 0		
			},
			loaded: false,
			suppliers: {} // 供应商
		}
	},
	getters: {
    amount(state){
			let total = 0
			state.view.items.forEach(item => {
				item.checked == 1 && (total += item.price * item.number)
			})
      return Number(total.toFixed(2))
    },
		count(state){
			let count = 0
			state.view.items.forEach(item => {
				item.checked == 1 && (count += item.number)
			})
			return Number(count)
		},
		total(state){
			let total = 0
			state.view.items.forEach(item => total += item.price * item.number)
			return Number(total)
		}
  },
	actions: {
		// 刷新数据
		update(state, values){
			for(const i in values){
				if(i === 'view'){
					Object.assign(state.view, values.view)
				} else {
					state[i] = values[i]
				}
			}
		}
	}
})
